#include "includes.h"

//0x03
//External Interrupt0
#ifdef USE_EXT0_ISR
void EXT0_ISR(void) interrupt 0
{
}
#endif	//USE_EXT0_ISR

//0x0B
//Timer0 Overflow
#ifdef USE_TIMER0_ISR
void TIMER0_ISR(void) interrupt 1
{
#ifdef TK_COMM
	static uint8_t t_1ms_cnt;
	static uint8_t t_250ms_cnt;
	static uint8_t t_1s_cnt;
	
	TCON &= ~BIT5;
	//250us
	t_1ms_cnt++;
	if(t_1ms_cnt>3)
	{
		t_1ms_cnt = 0;
		//1ms
		if(TkScanCnt<0xFF)
			TkScanCnt++;
		t_250ms_cnt++;
		if(t_250ms_cnt>249)
		{
			t_250ms_cnt = 0;
			//250ms
			t_1s_cnt++;
			if(t_1s_cnt>3)
			{
				t_1s_cnt = 0;
				if(TkFreshCnt<0xFF)
					TkFreshCnt++;
				//1s
			}
		}
	}
#endif	//TK_COMM

#ifdef PRF_UART
	static uint8_t t_1ms_cnt;
	static uint8_t t_250ms_cnt;
	static uint8_t t_1s_cnt;
	
	TCON &= ~BIT5;
	//250us
	if(Uart.rxsta==RXSTA_BUSY)
	{
		Uart.rxtm++;
		if(Uart.rxtm>8)
		{
			Uart.rxsta = RXSTA_FIN;
		}
	}
	t_1ms_cnt++;
	if(t_1ms_cnt>3)
	{
		t_1ms_cnt = 0;
		//1ms
		t_250ms_cnt++;
		if(t_250ms_cnt>249)
		{
			t_250ms_cnt = 0;
			//250ms
			t_1s_cnt++;
			if(t_1s_cnt>3)
			{
				t_1s_cnt = 0;
				//1s
			}
		}
	}
#endif	//PRF_UART
}
#endif	//USE_TIMER0_ISR

//0x13
//External Interrupt1
#ifdef USE_EXT1_ISR
void EXT1_ISR(void) interrupt 2
{
}
#endif	//USE_EXT1_ISR

//0x1B
//Timer1 Overflow
#ifdef USE_TIMER1_ISR
void TIMER1_ISR(void) interrupt 3
{
	TCON &= ~BIT7;
#ifdef ISS_DBG
	TH1 = 0x0F;
	TL1 = 0xFF;
	
	P25 = ~P25;
#endif	//ISS_DBG
}
#endif	//USE_TIMER1_ISR

//0x23
//Serial Port Interrupt
#ifdef USE_UART_ISR
void UART_ISR(void) interrupt 4
{
#ifdef PRF_UART
	uint8_t tmp;
	
	if(SCON&BIT0)										//RX
	{
		SCON &= ~BIT0;
		tmp = SBUF;
		if(Uart.rxsta==RXSTA_IDLE)
		{
			Uart.rxsta = RXSTA_BUSY;
			Uart.rxlen = 0;
		}
		if(Uart.rxsta==RXSTA_BUSY)
		{
			if(Uart.rxlen<UART_RXBUF_SIZE)
			{
				Uart.rxbuf[Uart.rxlen++] = tmp;
			}
			else
			{
				Uart.rxsta = RXSTA_FIN;
			}
			Uart.rxtm = 0;
		}
	}
#endif	//PRF_UART
}
#endif	//USE_UART_ISR

//0x2B
//Timer2 Overflow or external
#ifdef USE_TIMER2_ISR
void TIMER2_ISR(void) interrupt 5
{
	IRCON &= ~BIT6;
}
#endif	//USE_TIMER2_ISR

//0x33
//I2C Interrupt
#ifdef USE_I2C_ISR

void I2C_ISR(void) interrupt 6
{
	switch(I2CSTA)
	{
		//Slaver
		//////////////////////////////////////////////////
		//Read
		case 0xA8:
			I2c.rxlen = 0x00;
			I2CDAT = I2c.rxbuf[I2c.rxlen++];
			I2CCON = 0x44;
			break;
		case 0xB8:
			I2CDAT = I2c.rxbuf[I2c.rxlen++];
			I2CCON = 0x44;
			break;
		case 0xC8:
			I2CDAT = I2c.rxbuf[I2c.rxlen++];
			I2CCON = 0x40;
			break;
		//Write
		case 0x60:
			I2c.rxlen = 0x00;
			I2CCON = 0x44;
			break;
		case 0x80:
			I2c.rxbuf[I2c.rxlen++] = I2CDAT;
			I2CCON = 0x44;
			break;
		case 0x88:
			I2c.rxbuf[I2c.rxlen++] = I2CDAT;
			I2CCON = 0x40;
			break;
		case 0xA0:							//Stop
			I2CCON = 0x44;
			break;
		//////////////////////////////////////////////////
	}
	I2CCON &= ~BIT3;							//Clear interrupt flag
}
#endif	//USE_I2C_ISR

//0x3B
//Keyboard Interrupt
#ifdef USE_KB_ISR
void KB_ISR(void) interrupt 7
{
#ifdef USE_KB_DEMO
	SleepFlag = 0;
	KbDemoIsrProc();
#endif	//USE_KB_DEMO
#ifdef USE_KB_RMT_APP
	SleepFlag = 0;
	EKB = 0;
	RmtIsrProc();
#endif	//USE_KB_RMT_APP
}
#endif	//USE_KB_ISR

//0x43
//SPI Interrupt
#ifdef USE_SPI_ISR
void SPI_ISR(void) interrupt 8
{
}
#endif	//USE_SPI_ISR

//0x4B
//Touch Key / ADC Interrupt
#ifdef USE_TK_ADC_ISR
void TK_ADC_ISR(void) interrupt 9
{
#ifdef USE_ADC
	IRCON &= ~BIT5;
	AdcVal[AdcCh].u8[0] = TKDATH;
	AdcVal[AdcCh].u8[1] = TKDATL;
	TKADCF &= ~BIT3;                    //Stop ADC
	TKCON1 &= ~BIT7;										//Stop ADC
	TKCON0 &= ~BIT6;										//Disable ADC
	AdcFlag = 1;
#endif	//USE_ADC
#ifdef USE_TK
	IRCON &= ~BIT5;
	TkDat.u8[0] = TKDATH;
	TkDat.u8[1] = TKDATL;
	TKCON2 &= ~BIT2;
	TkWkUpFlag = 1;
	SleepFlag = 0;
#endif	//USE_TK
}
#endif	//USE_TK_ISR

//0x53
//Timer 2 Capture/Compare
#ifdef USE_T2CC_ISR
void T2CC_ISR(void) interrupt 10
{
}
#endif	//USE_T2CC_ISR

//0x5B
//EE Write Finshed Interrupt
#ifdef USE_PGMF_ISR
void PGMF_ISR(void) interrupt 11
{
}
#endif	//USE_PGMF_ISR

//0x63
//Comparator Interrupt
#ifdef USE_CMPF_ISR
void CMPF_ISR(void) interrupt 12
{
}
#endif	//USE_CMPF_ISR

//Reverse
//0x6B
//interrupt 13

//Reverse
//0x73
//interrupt 14

//0x7B
//WDT interrupt
#ifdef USE_WDTF_ISR
void WDTF_ISR(void) interrupt 15
{
	SleepFlag = 0;
}
#endif	//USE_WDTF_ISR



